﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Quan_ly_thong_tin_he_thong
{
    public partial class ThongTinKhachHang : Form
    {
        public ThongTinKhachHang()
        {
            InitializeComponent();
        }

        private void khoaThongTin()
        {
            txtHoTen.Enabled = false;
            txtMSKH.Enabled = false;
            radioButton1.Enabled = false;
            radioButton2.Enabled = false;
            NgayComboBox.Enabled = false;
            ThangCombobox.Enabled = false;
            NamCombobox.Enabled = false;
            txtNgheNhiep.Enabled = false;
            txtDiaChi.Enabled = false;
            txtCMND.Enabled = false;
            txtSDT.Enabled = false;
            txtEmail.Enabled = false;
            txtTenDangNhap.Enabled = false;
        }

        private void xoaThongTin()
        {
            txtHoTen.Text = null;
            txtMSKH.Text = null;
            radioButton1.Checked = false;
            radioButton2.Checked = false;
            NgayComboBox.Text = "1";
            ThangCombobox.Text = "1";
            NamCombobox.Text = "1991";
            txtNgheNhiep.Text = null;
            txtDiaChi.Text = null;
            txtCMND.Text = null;
            txtSDT.Text = null;
            txtEmail.Text = null;
            txtTenDangNhap.Text = null;
        }

        private void timKiemKhachHang()
        {
            Config file = new Config(Application.StartupPath + "\\Config.ini");
            string connectionstring = file.ReadValue("server", "con");
            SqlConnection conn = new SqlConnection(connectionstring);
            if (timKiemCombobox.Text == "Tất cả")
            {
                string sql = "select *,(case when GioiTinh_KH = '1' then 'Nam' else N'Nữ' end)AS GioiTinh from KhachHang";
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                adapter.Fill(ds, "KhachHang");

                dataGridView1.AutoGenerateColumns = false;
                dataGridView1.DataSource = ds.Tables["KhachHang"].DefaultView;                
            }
            else
                if (timKiemCombobox.Text == "Mã khách hàng")
                {
                    string sql = "select *,(case when GioiTinh_KH = '1' then 'Nam' else N'Nữ' end)AS GioiTinh from KhachHang where MaSo_KH = @MaSo_KH";
                    SqlCommand cmd = new SqlCommand(sql, conn);
                    cmd.Parameters.AddWithValue("@MaSo_KH", txttimKiem.Text);
                    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                    DataSet ds = new DataSet();
                    adapter.Fill(ds, "KhachHang");

                    dataGridView1.AutoGenerateColumns = false;
                    dataGridView1.DataSource = ds.Tables["KhachHang"].DefaultView;   
                }
            else
                    if (timKiemCombobox.Text == "Tên khách hàng")
                    {
                        string sql = "select *,(case when GioiTinh_KH = '1' then 'Nam' else N'Nữ' end)AS GioiTinh from KhachHang where HoTen_KH like '"+txttimKiem.Text+ "%'";
                        SqlCommand cmd = new SqlCommand(sql, conn);
                        //cmd.Parameters.AddWithValue("@HoTen_KH", noidung);
                        SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                        DataSet ds = new DataSet();
                        adapter.Fill(ds, "KhachHang");

                        dataGridView1.AutoGenerateColumns = false;
                        dataGridView1.DataSource = ds.Tables["KhachHang"].DefaultView;
                    }
        }

        private void loadDataGridViewMSKH(string MSKH)
        {
            Config file = new Config(Application.StartupPath + "\\Config.ini");
            string connectionstring = file.ReadValue("server", "con");
            SqlConnection conn = new SqlConnection(connectionstring);
            if (MSKH == "")
            {
                string sql = "select *,(case when GioiTinh_KH = '1' then 'Nam' else N'Nữ' end)AS GioiTinh from KhachHang";
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                adapter.Fill(ds, "KhachHang");
                dataGridView1.AutoGenerateColumns = false;
                dataGridView1.DataSource = ds.Tables["KhachHang"].DefaultView;
            }
            else
            {
                string sql = "select *,(case when GioiTinh_KH = '1' then 'Nam' else N'Nữ' end)AS GioiTinh from KhachHang where MaSo_KH = @MaSo_KH";
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("@MaSo_KH", MSKH);
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                adapter.Fill(ds, "KhachHang");
                dataGridView1.AutoGenerateColumns = false;
                dataGridView1.DataSource = ds.Tables["KhachHang"].DefaultView;
            }

        }

        private void docThongTinKhachHang(string MSKH)
        {
            Config file = new Config(Application.StartupPath + "\\Config.ini");
            string connectionstring = file.ReadValue("server", "con");
            SqlConnection conn = new SqlConnection(connectionstring);
            string sql = "select *,convert(char(10),NgaySinh_KH,103) as NgaySinh from KhachHang where MaSo_KH = @MaSo_KH";
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.Parameters.AddWithValue("@MaSo_KH", MSKH);
            conn.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            if (reader.Read())
            {
                txtMSKH.Text = reader["MaSo_KH"].ToString();
                txtHoTen.Text = reader["HoTen_KH"].ToString();
                if ((bool)reader["GioiTinh_KH"] == true)
                    radioButton1.Checked = true;
                else
                    radioButton2.Checked = true;
                txtCMND.Text = reader["CMND_KH"].ToString();
                NgayComboBox.Text = ((string)reader["NgaySinh"]).Split('/')[0];
                ThangCombobox.Text = ((string)reader["NgaySinh"]).Split('/')[1];
                NamCombobox.Text = ((string)reader["NgaySinh"]).Split('/')[2];
                txtNgheNhiep.Text = reader["NgheNghiep_KH"].ToString();
                txtDiaChi.Text = reader["DiaChi_KH"].ToString();
                txtSDT.Text = reader["SDT_KH"].ToString();
                txtEmail.Text = reader["Email_KH"].ToString();
                txtTenDangNhap.Text = reader["TenDangNhap_KH"].ToString();
            }
            reader.Close();
            conn.Close();
        }

        private void xoaKhachHang(string MSKH)
        {
            Config file = new Config(Application.StartupPath + "\\Config.ini");
            string connectionstring = file.ReadValue("server", "con");
            SqlConnection conn = new SqlConnection(connectionstring);
            string sql = "delete from KhachHang where MaSo_KH = @MaSo_KH";
            SqlCommand cmd = new SqlCommand(sql, conn);
            conn.Open();
            cmd.Parameters.AddWithValue("@MaSo_KH", MSKH);
            cmd.ExecuteNonQuery();
            conn.Close();
        }

        //private void docThongTin(string MSKH)
        //{
        //    Config file = new Config(Application.StartupPath + "\\Config.ini");
        //    string connectionstring = file.ReadValue("server", "con");
        //    SqlConnection conn = new SqlConnection(connectionstring);
        //    string sql = "select * from KhachHang where MaSo_KH" = 
        //}

        private void ThongTinKhachHang_Load(object sender, EventArgs e)
        {
            loadDataGridViewMSKH("");
            NgayComboBox.Text = "1";
            ThangCombobox.Text = "1";
            NamCombobox.Text = "1991";
            khoaThongTin();
        }

        private void ThoatButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            string MSKH;
            MSKH = dataGridView1.Rows[e.RowIndex].Cells["MaSo_KH1"].Value.ToString();
            docThongTinKhachHang(MSKH);
        }

        private void TimButton_Click(object sender, EventArgs e)
        {
            timKiemKhachHang();
        }

        private void XoaButton_Click(object sender, EventArgs e)
        {
            xoaKhachHang(txtMSKH.Text);
            xoaThongTin();
            loadDataGridViewMSKH("");
        }
        
    }
}
